package com.sec.android.app.myfiles.module.abstraction;

import android.content.Context;
import android.database.Cursor;

import com.sec.android.app.myfiles.MyFilesTestRunner;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.mockclass.CursorMock;
import com.sec.android.app.myfiles.mockclass.FileMock;
import com.sec.android.app.myfiles.module.local.file.LocalFileRecord;
import com.sec.android.app.myfiles.shadow.android.ShadowContentResolver;
import com.sec.android.app.myfiles.shadow.sem.ShadowSemPrivateModeManager;
import com.sec.android.app.myfiles.util.SemFwWrapper;
import com.sec.android.app.myfiles.util.StorageMonitor;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;

import java.util.ArrayList;

@RunWith(MyFilesTestRunner.class)
public class testLocalFileRecord {
    private LocalFileRecord mTestRecord;
    private Context mContext;

    static final String TEST_PARENT_PATH = AppConstants.StoragePath.INTERNAL_ROOT + "/testDir";
    static final String TEST_NAME = "TestFile.txt";
    static final String TEST_PATH = TEST_PARENT_PATH + "/" + TEST_NAME;


    @Before
    public void setUp() throws Exception {
        mContext = RuntimeEnvironment.application;
        mTestRecord = new LocalFileRecord(TEST_PATH);
        ShadowSemPrivateModeManager.init();
    }

    @After
    public void tearDown() throws Exception {
        ShadowContentResolver.setCursor(null);
    }

    @Test
    public void testIsRoot() {
        Assert.assertFalse(mTestRecord.isRoot());

        ArrayList<FileRecord> list = new ArrayList<>();

        list.add(new LocalFileRecord(AppConstants.StoragePath.ROOT + "/a"));
        list.add(new LocalFileRecord(AppConstants.StoragePath.ROOT + "/b"));
        list.add(new LocalFileRecord(AppConstants.StoragePath.PRIVATE_ROOT));
        list.add(new LocalFileRecord(AppConstants.StoragePath.PRIVATE_ROOT));
        list.add(new LocalFileRecord(StorageMonitor.getExtSDCardPath()));

        for (FileRecord record : list) {
            Assert.assertTrue(record.getFullPath(), record.isRoot());
        }
    }

    @Test
    public void testGetPath() {
        Assert.assertEquals(TEST_PARENT_PATH, mTestRecord.getPath());
    }

    @Test
    public void testGetName() {
        Assert.assertEquals(TEST_NAME, mTestRecord.getName());
    }

    @Test
    public void testGetFullPath() {
        Assert.assertEquals(TEST_PATH, mTestRecord.getFullPath());
    }


    @Test
    public void testGetParent() {
        FileRecord parent = mTestRecord.getParent();
        Assert.assertNotNull(parent);
        Assert.assertEquals(TEST_PARENT_PATH, parent.getFullPath());
    }

    @Test
    public void testChildFileCount() {
        int actual[] = LocalFileRecord.getChildFileCount(SemFwWrapper.file(TEST_PARENT_PATH));
        Assert.assertEquals(FileMock.LIST_FILES_SIZE, actual[LocalFileRecord.WITH_HIDDEN]);
        Assert.assertEquals(FileMock.LIST_FILES_SIZE - FileMock.NUM_OF_HIDDEN_FILES, actual[LocalFileRecord.WITHOUT_HIDDEN]);
    }

    @Test
    public void testGetItemCount() {
        LocalFileRecord record = new LocalFileRecord(TEST_PARENT_PATH);
        int actual = record.getItemCount(true);
        Assert.assertEquals(FileMock.LIST_FILES_SIZE, actual);
    }


    @Test
    public void testExists() {
        Assert.assertTrue(mTestRecord.exists(mContext));

        LocalFileRecord record = new LocalFileRecord(AppConstants.StoragePath.PRIVATE_ROOT + "/file.txt");
        Assert.assertTrue(record.exists(mContext));
        ShadowSemPrivateModeManager.mIsReady = false;
        Assert.assertFalse(record.exists(mContext));
    }

    @Test
    public void testGetRealMediaId() {
        ShadowContentResolver.setCursor(null);
        int expected = -1;
        int actual = mTestRecord.getRealMediaId(mContext);
        Assert.assertEquals(expected, actual);

        expected = 10;
        Cursor cursorMock = CursorMock.createCursor(1);
        CursorMock.addIntValue(cursorMock, 0, expected);
        ShadowContentResolver.setCursor(cursorMock);
        actual = mTestRecord.getRealMediaId(mContext);
        Assert.assertEquals(expected, actual);
    }
}
